package com.myproject.http;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description: //TODO  简单的HTTP服务器， 其实就是接受HTTP请求，并解析请求，
 * @Author: sunfch
 * @Date: 2019/1/3 16:35
 * @Param:
 * @Return:
 */
public class HttpServer2 {

    public static void main(String[] args) {
        try {

            /*监听端口号，只要是8888就能接收到*/
            ServerSocket ss = new ServerSocket(9960);

            while (true) {
                /*实例化客户端，固定套路，通过服务端接受的对象，生成相应的客户端实例*/
                Socket socket = ss.accept();
                /*获取客户端输入流，就是请求过来的基本信息：请求头，换行符，请求体*/
                InputStream inputStream = socket.getInputStream();
                System.out.println(inputStream.markSupported());
                int b = 0;
                OutputStream outputStream = new FileOutputStream("E:\\abc.txt");
                while ((b=inputStream.read())!=-1){
                    outputStream.write(b);
                }

                inputStream.close();
                outputStream.close();
                socket.close();
                System.out.println(socket+": close");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}